package org.clevernote.main;

import java.util.List;

import org.clevernote.foundations.ClevernoteConstants;
import org.clevernote.foundations.Preferences;
import org.clevernote.foundations.cmd.Cmd;
import org.clevernote.foundations.EvernoteExtension;
import org.clevernote.foundations.Authentication;
import org.clevernote.foundations.StringConstants;

import ui.cmdline.parser.CommandLineArguments;
import ui.cmdline.parser.ParserException;
import ui.cmdline.parser.types.*;

import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.notestore.NoteStore;
import com.evernote.edam.notestore.NoteList;
import com.evernote.edam.notestore.NoteFilter;
import com.evernote.edam.type.Notebook;
import com.evernote.edam.type.Note;
import com.facebook.thrift.TException;
import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.transport.THttpClient;



public class CmdListNotes implements Cmd {
	
	

	public void run(CommandLineArguments cmdline,Authentication auth) throws TException, EDAMSystemException, EDAMUserException {
		
		THttpClient noteStoreTrans = new THttpClient(auth.getNoteStoreUrl());
		TBinaryProtocol noteStoreProt = new TBinaryProtocol(noteStoreTrans);
		NoteStore.Client noteStore = new NoteStore.Client(noteStoreProt,noteStoreProt);
		
		String authToken = auth.getAuthenticationToken();
		
		String notebookGuid = EvernoteExtension.getNotebookGuid(
				cmdline.getString(StringConstants.NOTEBOOK,Preferences.retreive(StringConstants.NOTEBOOK)),
				noteStore,
				authToken
				);
		
		NoteFilter filter = new NoteFilter();
		filter.setNotebookGuid(notebookGuid);
		
		EvernoteExtension.NoteVisitor visitor = new EvernoteExtension.NoteVisitor() {
			public void visitNote(Note _note) {
				System.out.println(" * " + _note.getTitle());
			}
		};
		
		EvernoteExtension.findNotes(noteStore, authToken, filter, visitor);	
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CmdListNotes cmd = new CmdListNotes();
			CmdRunner.executeCmd(args, cmd) ;			
		} catch (Exception e) {
			e.printStackTrace();
		} 

	}

	public String getName() {
		return StringConstants.CMD_LIST_NOTES;
	}

}
